<?php
header('Content-Type: text/xml');
$sensor_id=$_GET["sensor_id"];
$start=$_GET["start"];
$stop=$_GET["stop"];
$type = $_GET["type"];




$start_date= date('Y-m-d H:i:s',$start);
$stop_date= date('Y-m-d H:i:s', $stop);

$con = mysql_connect("localhost", "root", "bytewalla5");
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("wsn", $con);

// select last 5 entries
//SELECT distinct timestamp,light FROM data WHERE spot_id="30" AND timestamp between "2011-11-30 08:15:41" and "2011-11-30 08:16:05" order by timestamp
$query = "SELECT distinct timestamp,".$type." FROM data WHERE spot_id=\"$sensor_id\" AND timestamp between \"$start\" and \"$stop\" order by timestamp ";
//echo $query;
$res = mysql_query($query);
 
// put all the data inside an array
$arr = array();
while($obj = mysql_fetch_object($res)){
  $arr[] = $obj;
}
mysql_close($con);
 
// echo xml
echo "<"."?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";
echo "<chart>\n"; 
echo "<series>\n"; 
for ($i = 1; $i <= sizeof($arr); $i++) {  
   $time = $arr[$i-1]->timestamp;  
  echo "<value xid=\"$i\">$time</value>\n";
}
echo "</series>\n";



echo "<graphs>\n";
echo "<graph title=\"TDS\" >\n";
for ($i = 1; $i<=sizeof($arr); $i++) {  
  $readings = $arr[$i-1]->$type; 
  echo "<value xid=\"$i\">$readings</value>\n";
}
echo "</graph>\n";


echo "</graphs>\n";
echo "</chart>";
 
?>
	